<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData,subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,errorModal,ptFloat,minusF,getDateStr,timeCompare,getBracketStr,getDayOfWeek} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				uId:"",
				attendanceNodeCache:"hr-kq-repair-attendanceNode-cache",
				starttime:"",
				endtime:"",
				edit:"",
				subIndex:''
			})
		},
		methods:{
			verifyAttendanceNode(){
				let _that = this
				if(!_that.$refs.autoform.formdata.supplementDate){
					let title = _that.$refs.autoform.getEditorTitle("dataInfo","supplementDate")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			onSelectTime(time){
			},
			getClockRecord(date){
				let _that = this
				_that.om.request(
					"tsupplementAttendance/getClockRecord",
					{
						
						userId:_that.uId,
						date:date
					}
				).then(res=>{
					if(!res.succeed){
						_that.om.toast(res.msg)
						_that.$refs.autoform.clear("dataInfo","supplementTime")
					}else{
						_that.$refs.autoform.formdata.attendanceRecord = res.value
					}
				})
			},
			verifyUser(date){
				let _that = this
				_that.om.request(
					"tsupplementAttendance/verifyUser",
					{
						userId:_that.uId,
						date:date
					}
				).then(res=>{
					if(!res.succeed){
						_that.om.toast(res.msg)
						_that.$refs.autoform.clear("dataInfo","supplementTime")
					}
				})
			},
			changeAttendanceNode(item){
				let _that = this
				_that.$refs.autoform.changeEditor("dataInfo","attendanceNodeName",{
					link:'../../public/search/search',
					"linkParam":{
						other:JSON.stringify({
							userId: _that.uId,
							date:item.value
						}),
						url:'tsupplementAttendance/getRepairNodeList',
						namefield:"nodeName",
						extrafield:"canRepair",
						notefield:'tip',
						initload:true,
						cachename:_that.attendanceNodeCache
					}
				})
			},
			onSelectDate(item){
				let _that = this
				_that.verifyUser(item.value)
				_that.getClockRecord(item.value)
				_that.changeAttendanceNode(item)
				_that.$refs.autoform.clear("dataInfo","attendanceNodeName")
				_that.$refs.autoform.formdata.week = getDayOfWeek(item.value)
			},
			
			initLoad(){
				let _that = this
				let pro1 = _that.om.request("tsupplementAttendance/getDateList",{userId:_that.uId})
				let pro2 = _that.om.request("tsysdict/getListByCode",{code:'38037'})
				Promise.all([pro1,pro2]).then(res=>{
					_that.$refs.autoform.changeEditor("dataInfo","supplementDate","data",configSelectData(res[0],"name",'value'))
					_that.$refs.autoform.initSelectIndex("dataInfo","supplementDate")
					_that.$refs.autoform.changeEditor("dataInfo","reasonId","data",configSelectData(res[1],"id",'dicname'))
					_that.$refs.autoform.initSelectIndex("dataInfo","reasonId")
				})
			}
		},
		onShow() {
			let _that = this
			
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.attendanceNodeCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							if(d.tip){
								_that.om.toast(d.tip)
								uni.removeStorage({
									key:_that.attendanceNodeCache
								})
								return
							}
							let obj = {
								isSame:false,
								index:0
							}
							for(let i in _that.datas){
								if(i == Number(_that.subIndex)){
									continue;
								}
								if(_that.datas[i].supplementDate+_that.datas[i].attendanceNodeName ==
									_that.$refs.autoform.formdata.supplementDate+d.nodeSimpleName){
									obj = {
										isSame:true,
										index:i
									}
								}
							}
							if(!obj.isSame){
								formdata.attendanceNodeName = d.nodeSimpleName
								_that.$refs.autoform.changeEditor("dataInfo","supplementTime",'min',d.starttime)
								_that.$refs.autoform.changeEditor("dataInfo","supplementTime",'max',d.endtime)
								_that.$refs.autoform.clear("dataInfo","supplementTime")
								_that.starttime = d.starttime
								_that.endtime = d.endtime
								_that.$refs.autoform.formdata.nodeStartTime = d.starttime
								_that.$refs.autoform.formdata.nodeEndTime = d.endtime
								_that.$refs.autoform.formdata.supplementTime = d.stdtime
							}else{
								errorModal(_that,"提示信息","选择的“"+_that.$refs.autoform.formdata.supplementDate+d.nodeSimpleName+"”和第"+(Number(obj.index)+1)+'条重复,请勿重复选择！',()=>{})
							}
							uni.removeStorage({
								key:_that.attendanceNodeCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.uId = e.userId
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
								_that.initLoad()
								_that.edit = e.edit
								_that.subIndex = e.index
							}
						}
					})
				}
			})
			_that.$nextTick(function(){
				if(_that.$refs.autoform.formdata.nodeStartTime && _that.$refs.autoform.formdata.nodeEndTime){
					_that.starttime =_that.$refs.autoform.formdata.nodeStartTime
					_that.endtime = _that.$refs.autoform.formdata.nodeEndTime
					_that.$refs.autoform.changeEditor("dataInfo","supplementTime",'min',_that.starttime)
					_that.$refs.autoform.changeEditor("dataInfo","supplementTime",'max',_that.endtime)
					_that.changeAttendanceNode({value:_that.$refs.autoform.formdata.supplementDate})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
